package com.fdb.efp.esb.service.client.impl;

import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.DAY;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.HOUR;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.MIN;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.MONTH;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.SECOND;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.SEQ;
import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.YEAR_4;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.fdb.basic.sequence.service.DefaultPatternDescriptor;
import com.fdb.basic.sequence.service.PatternDescriptor;
import com.fdb.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.fdb.efp.esb.common.client.req.EsbReqAppHeadBean;
import com.fdb.efp.esb.common.client.req.EsbReqServiceBean;
import com.fdb.efp.esb.common.client.req.EsbReqSysHeadBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.basic.framework.core.util.SpringContextUtils;

/**
 * @description:ESB客户端报文创建器
 * @date:2018年12月19日 下午2:08:40
 * @author:hengyh
 */
public class ESBBeanCreator {
	
	private static final PatternDescriptor pattern = 
			DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo + 
					YEAR_4 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);

	/**
	 * @description:创建EsbReqServiceBean用于发送ESB请求
	 * @param serverCode
	 *            服务方代码
	 * @param scene
	 *            应用场景
	 * @param body
	 *            请求体
	 * @author:hengyh
	 */
	public static EsbReqServiceBean create(String serverCode, String scene, Object body) throws Exception {
		EsbReqSysHeadBean sysHead = buildSysHead(serverCode, scene);
		EsbReqAppHeadBean appHead = buildAppHead();
		EsbReqServiceBean reqBean = new EsbReqServiceBean();
		reqBean.setAppHead(appHead);
		reqBean.setSysHead(sysHead);
		reqBean.setBODY(body);
		return reqBean;
	}
	
	/**----------------------------------------------------**/
	/**                   private methods                  **/
	/**----------------------------------------------------**/

	private static DateTimeFormatter dateForm = DateTimeFormatter.ofPattern("yyyyMMdd");
	private static DateTimeFormatter timeForm = DateTimeFormatter.ofPattern("HHmmss");
	
	private static EsbReqSysHeadBean buildSysHead(String serverCode, String scene) throws Exception {
		EsbReqSysHeadBean syshead = new EsbReqSysHeadBean();
		syshead.setSvcCd(serverCode); // 要调用的服务代码
		syshead.setScnCd(scene); // 服务的应用场景
		syshead.setCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
		syshead.setOrgnlCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
		String esbSerialNo ;
		syshead.setCnsmrSrlNo(esbSerialNo = generateESBTradeSerialNo()); // 消费方流水号
		syshead.setGlblSrlNo(esbSerialNo); // 全局流水号
		LocalDateTime date = LocalDateTime.now();
		syshead.setTxnDt(dateForm.format(date));
		syshead.setTxnTm(timeForm.format(date));
		syshead.setTxnMd(ESBClientConstance.TxnMd.ONLINE.name());
		syshead.setUsrLng(ESBClientConstance.UsrLng.CHINESE.name());
		syshead.setFileFlg(ESBClientConstance.FileFlg.NONE.VALUE);
		syshead.setEryptMd(ESBClientConstance.EryptMd.NONE.VALUE);
		syshead.setPlafmID(ESBClientConstance.PlafmID);
		syshead.setCompany(ESBClientConstance.Company);

		syshead.setOrgnlTmlIdNo("");
		syshead.setTmlIdNo("");
		syshead.setOrgnlCnsmrSvcNo("");
		syshead.setCnsmrSvcNo("");
		syshead.setMACVal("");
		syshead.setMACFctr("");
		syshead.setPINSd("");
		syshead.setSvcVerNo("");
		syshead.setVerfFlg("");
		syshead.setMsgTp("");
		syshead.setPltfmClntNo("");
		syshead.setPlafmUsrID("");
		syshead.setPrdctID("");
		syshead.setRelGlblSrlNo("");
		return syshead;
	}

	private static EsbReqAppHeadBean buildAppHead() {
		EsbReqAppHeadBean appHead = new EsbReqAppHeadBean();
		appHead.setInstId(ESBClientConstance.InstId);
		appHead.setUsrNo(ESBClientConstance.UsrNo);
		appHead.setCnlTp(ESBClientConstance.CnlTp);
		
		appHead.setUsrPswd("");
		appHead.setUsrLvl("");
		appHead.setUsrTp("");
		appHead.setTlrSrlNo("");
		appHead.setUsrTp("");
		appHead.setCnlDtlTp("");
		appHead.setCnsmrTxnCd("");
		return appHead;
	}

	/* 生成ESB系统的交易流水号(CnsmrSrlNo) */
	private static String generateESBTradeSerialNo() throws Exception {
		PatternedLimitableSeqService sequenceService = (PatternedLimitableSeqService) SpringContextUtils.getBean("defaultSeqService");
		return sequenceService.getPatternedSequence("ESB", pattern);
	}
}
